package com.zjitc.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjitc.common.exception.AuthException;
import com.zjitc.serviceedu.entity.EduSubject;
import com.zjitc.serviceedu.entity.excel.ExcelSubjectData;
import com.zjitc.serviceedu.entity.excel.SubjectExcelListener;
import com.zjitc.serviceedu.entity.subject.OneSubject;
import com.zjitc.serviceedu.entity.subject.TwoSubject;
import com.zjitc.serviceedu.mapper.EduSubjectMapper;
import com.zjitc.serviceedu.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-09-20
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
//poi读取excel内容


    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            //1 获取文件输入流
            InputStream inputStream = file.getInputStream();

            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch(Exception e) {
            e.printStackTrace();
            throw new AuthException("添加课程分类失败");
        }
    }

    @Override
    public List<OneSubject> getAllOneAndTwoSubject() {
            // 查询所有一级分类
    QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", 0L);
    List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

    // 查询所有二级分类
    QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", 0L);
    List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

    // 使用Stream API来封装一级分类和二级分类
    List<OneSubject> finalSubjectList = oneSubjectList.stream()
            .map(oneSubject -> {
                OneSubject one = new OneSubject();
                one.setId(oneSubject.getId());
                one.setTitle(oneSubject.getTitle());
                one.setChildren(twoSubjectList.stream()
                        .filter(twoSubject -> twoSubject.getParentId().equals(oneSubject.getId()))
                        .map(twoSubject -> {
                            TwoSubject two = new TwoSubject();
                            two.setId(twoSubject.getId());
                            two.setTitle(twoSubject.getTitle());
                            return two;
                        })
                        .collect(Collectors.toList()));
                return one;
            })
            .collect(Collectors.toList());

        return finalSubjectList;
    }

    @Override
    public List<TwoSubject> getTwoSubjectsByParentId(Long parentId) {
        // 根据提供的父级分类ID来查询二级分类
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        List<EduSubject> eduSubjectList = baseMapper.selectList(wrapper);

        // 将查询到的EduSubject对象转换为TwoSubject对象列表
        return eduSubjectList.stream()
                .map(eduSubject -> {
                    TwoSubject twoSubject = new TwoSubject();
                    twoSubject.setId(eduSubject.getId());
                    twoSubject.setTitle(eduSubject.getTitle());
                    // 如果还有其他需要转换的字段，请在这里添加
                    return twoSubject;
                })
                .collect(Collectors.toList());
    }
}
